<?php

class Productimage extends E_ARB {

	protected $belongs_to = array('product', 'defaultimage');

	//need to fix this...this is not what initialise if for!
	function initialize($constructor_vars) {
		if((isset($constructor_vars['error'])) && ($constructor_vars['error'] == 0) && ($constructor_vars['size'] > 0) && ($constructor_vars['type'] == "image/jpeg") && (is_uploaded_file($constructor_vars['tmp_name']))) {
			$this->name = sha1($constructor_vars['name']);
			$this->tmp_name = $constructor_vars['tmp_name'];
			return $this;
		}
		return $this->set_attributes($constructor_vars); // dont know how it works but it does!
	}


	function before_save() {
		$temp_name = $this->tmp_name;
		unset($this->tmp_name);

		$filename = $this->name .".jpg";


	// --------------------------on full screen page begin

		$im = new Imagick($temp_name);
		$wide = $im->getImageWidth() >= $im->getImageHeight();

		if ($wide) {
			//$im->thumbnailImage($maxWidth, 0, false);
			//$im->scaleImage(640,0, false);
			//$im->adaptiveResizeImage(640,0, false);
			$width = 640;
			$canvas_height = 480;
			$im->resizeImage($width,0,Imagick::FILTER_LANCZOS,1, false);
			$height = ($im->getImageHeight()>$canvas_height) ? $im->getImageHeight() : $canvas_height;
		}
		else {
			//$im->adaptiveResizeImage(0,640, false);	
			//$im->scaleImage(0,640, false);
			//$im->thumbnailImage(0, $maxHeight, false);
			$canvas_width = 480;
			$height =  640;
			$im->resizeImage(0,$height,Imagick::FILTER_LANCZOS,1, false);
			$width =  ($im->getImageWidth()>$canvas_width) ? $im->getImageWidth() : $canvas_width;	
		}
		$canvas = new Imagick();
		$canvas->newImage( $width, $height, 'white', 'jpeg' );
		$geometry = $im->getImageGeometry();
		$x = ( $width - $geometry['width'] ) / 2;
		$y = ( $height - $geometry['height'] ) / 2;
		$canvas->compositeImage( $im, imagick::COMPOSITE_OVER, $x, $y );

		$draw = new ImagickDraw();
		$draw->setFont( ROOT . "components/fonts/SociaLAnimaL.ttf" );
		$draw->setFillColor("red");
		$draw->setFontSize( 50 );
		$draw->setFillOpacity( 0.5 );
		$draw->setGravity( Imagick::GRAVITY_CENTER );
		$canvas->annotateImage( $draw, 0, 0, 30, "GAMECITYJA.INFO" );


		$outfilename = "images/full/" . $filename;
		if (!$canvas->writeImage($outfilename)) {
			return false;
		}
		$canvas->destroy();
		$im->destroy();

// --------------------------on full screen page end

		$thumbs = array(
			"vps" => 
				array(
					"width" => 40,
					"height" => 35
				),
			"vpl" => 
				array(
					"width" => 180,
					"height" => 155
				),
			"thumb" => 
				array(
					"width" => 125,
					"height" => 105
				),
			"pico_full" => 
				array(
					"width" => 60,
					"height" => 46
				)
		);

		if ($wide) {
			foreach($thumbs as $folder_name => $thumb) {
				$img = new Imagick($temp_name);
				$wid = $thumb['width'];
				$hgt = $thumb['height'];
				$img->thumbnailImage($wid, $hgt);
				$outfilename = "images/{$folder_name}/{$filename}";
				if (!$img->writeImage($outfilename)) {
					return false;
				}
				$img->destroy();
			}
		}
		else {
			foreach($thumbs as $folder_name => $thumb) {
				$outfilename = "images/{$folder_name}/{$filename}";
				$img = new Imagick($temp_name);
				$wid = $thumb['width'];
				$hgt = $thumb['height'];
				if ($folder_name == 'vps') {
					$img->cropThumbnailImage($wid,$hgt);
					if (!$img->writeImage($outfilename)) {
						return false;
					}
					$img->destroy();
				}
				else {
					$img->thumbnailImage($wid,$hgt, true );
					$canvas = new Imagick();
					$canvas->newImage( $wid, $hgt, 'white', 'jpeg' );
					$geometry = $img->getImageGeometry();
					$x = ( $wid - $geometry['width'] ) / 2;
					$y = ( $hgt - $geometry['height'] ) / 2;
					$canvas->compositeImage( $img, imagick::COMPOSITE_OVER, $x, $y );
					if (!$canvas->writeImage($outfilename)) {
						return false;
					}
					$canvas->destroy();
					$img->destroy();
				}
			}
		}
		return true;
	}





}

?>